import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/actionsheet/brn_common_action_sheet.dart';
import 'package:smart_devices/bruno/src/theme/base/brn_text_style.dart';
import 'package:smart_devices/bruno/src/theme/configs/brn_action_sheet_config.dart';
import 'package:smart_devices/icons/src/antd_icons.dart';
import 'package:smart_devices/params/spare_part.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';

import 'screen_logic.dart';

class DeviceSpareScreenView extends GetView<DeviceSpareScreenLogic> {
  const DeviceSpareScreenView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor:
          ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 15,
          ),
          onPressed: Get.back,
        ),
        automaticallyImplyLeading: false,
        backgroundColor:
            ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
        title: Text(
          '筛选',
          style: StylesUtils.customTextStyle(
              fontSize: 18, fontWeight: FontWeight.w700),
        ),
        centerTitle: true,
        systemOverlayStyle: SystemUiOverlayStyle.dark,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
        child: Column(
          children: [
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '备件编号',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                    width: maxWidth,
                    alignment: Alignment.centerLeft,
                    child: Obx(() => TextField(
                          textDirection: TextDirection.ltr,
                          controller: TextEditingController(
                              text: controller.params.value.code),
                          cursorColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          style: StylesUtils.customTextStyle(
                              fontSize: 16, color: ColorsUtils.color1c24),
                          textAlign: TextAlign.left,
                          textAlignVertical: TextAlignVertical.center,
                          cursorWidth: 2,
                          cursorRadius: const Radius.circular(2),
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StylesUtils.customTextStyle(
                                  color: ColorsUtils.colorCccc, fontSize: 15),
                              isDense: true,
                              isCollapsed: true,
                              fillColor: Colors.white,
                              border: InputBorder.none,
                              focusColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              contentPadding:
                                  const EdgeInsets.symmetric(vertical: 20),
                              focusedBorder: InputBorder.none),
                          onChanged: (String text) {
                            controller.params.value.code = text;
                          },
                        )),
                  )
                ],
              )),
            ),
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '设备名称',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                    width: maxWidth,
                    alignment: Alignment.centerLeft,
                    child: Obx(() => TextField(
                          textDirection: TextDirection.ltr,
                          controller: TextEditingController(
                              text: controller.params.value.name),
                          cursorColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          style: StylesUtils.customTextStyle(
                              fontSize: 16, color: ColorsUtils.color1c24),
                          textAlign: TextAlign.left,
                          textAlignVertical: TextAlignVertical.center,
                          cursorWidth: 2,
                          cursorRadius: const Radius.circular(2),
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StylesUtils.customTextStyle(
                                  color: ColorsUtils.colorCccc, fontSize: 15),
                              isDense: true,
                              isCollapsed: true,
                              fillColor: Colors.white,
                              border: InputBorder.none,
                              focusColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              contentPadding:
                                  const EdgeInsets.symmetric(vertical: 20),
                              focusedBorder: InputBorder.none),
                          onChanged: (String text) {
                            controller.params.value.name = text;
                          },
                        )),
                  )
                ],
              )),
            ),
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '设备类型',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                    width: maxWidth,
                    alignment: Alignment.centerLeft,
                    child: Obx(() => TextField(
                          textDirection: TextDirection.ltr,
                          readOnly: true,
                          controller: controller.typeController.value,
                          cursorColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          style: StylesUtils.customTextStyle(
                              fontSize: 16, color: ColorsUtils.color1c24),
                          textAlign: TextAlign.left,
                          textAlignVertical: TextAlignVertical.center,
                          cursorWidth: 2,
                          cursorRadius: const Radius.circular(2),
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StylesUtils.customTextStyle(
                                  color: ColorsUtils.colorCccc, fontSize: 15),
                              isDense: true,
                              isCollapsed: true,
                              fillColor: Colors.white,
                              border: InputBorder.none,
                              focusColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              contentPadding:
                                  const EdgeInsets.symmetric(vertical: 20),
                              suffixIcon: Icon(
                                AntdIcons.right,
                                color: ColorsUtils.parseColorFromHexString(
                                    ColorsUtils.color9999),
                                size: 15,
                              ),
                              focusedBorder: InputBorder.none),
                          onTap: () {
                            showModalBottomSheet(
                                context: context,
                                backgroundColor: Colors.transparent,
                                builder: (BuildContext context) {
                                  return BrnCommonActionSheet(
                                    actions: SparePartTypeEnum.values
                                        .map((e) => BrnCommonActionSheetItem(
                                            e.label,
                                            titleStyle:
                                                StylesUtils.customTextStyle(
                                                    fontSize: 16,
                                                    color: e.label ==
                                                            controller
                                                                .params
                                                                .value
                                                                .type
                                                                ?.label
                                                        ? ColorsUtils.color0052
                                                        : ColorsUtils
                                                            .color3333)))
                                        .toList(),
                                    cancelTitle: '取消',
                                    themeData: BrnActionSheetConfig(
                                        cancelStyle: BrnTextStyle(
                                          color: ColorsUtils
                                              .parseColorFromHexString(
                                                  ColorsUtils.colorE34d),
                                          fontWeight: FontWeight.normal,
                                          fontSize: 14,
                                        ),
                                        topRadius: 10),
                                    clickCallBack: (int index,
                                        BrnCommonActionSheetItem actionItem) {
                                      controller.params.value.type =
                                          SparePartTypeEnum.values.firstWhere(
                                              (element) =>
                                                  element.label ==
                                                  actionItem.title);
                                      controller.typeController.value.text =
                                          actionItem.title;
                                    },
                                  );
                                });
                          },
                        )),
                  )
                ],
              )),
            ),
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '备件单位',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                    width: maxWidth,
                    alignment: Alignment.centerLeft,
                    child: Obx(() => TextField(
                          textDirection: TextDirection.ltr,
                          readOnly: true,
                          controller: controller.unitController.value,
                          cursorColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          style: StylesUtils.customTextStyle(
                              fontSize: 16, color: ColorsUtils.color1c24),
                          textAlign: TextAlign.left,
                          textAlignVertical: TextAlignVertical.center,
                          cursorWidth: 2,
                          cursorRadius: const Radius.circular(2),
                          decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: StylesUtils.customTextStyle(
                                  color: ColorsUtils.colorCccc, fontSize: 15),
                              isDense: true,
                              isCollapsed: true,
                              fillColor: Colors.white,
                              border: InputBorder.none,
                              focusColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              contentPadding:
                                  const EdgeInsets.symmetric(vertical: 20),
                              suffixIcon: Icon(
                                AntdIcons.right,
                                color: ColorsUtils.parseColorFromHexString(
                                    ColorsUtils.color9999),
                                size: 15,
                              ),
                              focusedBorder: InputBorder.none),
                          onTap: () {
                            showModalBottomSheet(
                                context: context,
                                backgroundColor: Colors.transparent,
                                builder: (BuildContext context) {
                                  return BrnCommonActionSheet(
                                    actions: SparePartUnitEnum.values
                                        .map((e) => BrnCommonActionSheetItem(
                                            e.label,
                                            titleStyle:
                                                StylesUtils.customTextStyle(
                                                    fontSize: 16,
                                                    color: e.label ==
                                                            controller
                                                                .params
                                                                .value
                                                                .type
                                                                ?.label
                                                        ? ColorsUtils.color0052
                                                        : ColorsUtils
                                                            .color3333)))
                                        .toList(),
                                    cancelTitle: '取消',
                                    themeData: BrnActionSheetConfig(
                                        cancelStyle: BrnTextStyle(
                                          color: ColorsUtils
                                              .parseColorFromHexString(
                                                  ColorsUtils.colorE34d),
                                          fontWeight: FontWeight.normal,
                                          fontSize: 14,
                                        ),
                                        topRadius: 10),
                                    clickCallBack: (int index,
                                        BrnCommonActionSheetItem actionItem) {
                                      controller.params.value.unit =
                                          SparePartUnitEnum.values.firstWhere(
                                              (element) =>
                                                  element.label ==
                                                  actionItem.title);
                                      controller.unitController.value.text =
                                          actionItem.title;
                                    },
                                  );
                                });
                          },
                        )),
                  )
                ],
              )),
            ),
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '当前库存',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                      width: maxWidth,
                      alignment: Alignment.centerLeft,
                      child: Row(children: [
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  keyboardType: TextInputType.number,
                                  controller: TextEditingController(
                                      text: controller
                                                  .params.value.minInventory !=
                                              null
                                          ? controller.params.value.minInventory
                                              .toString()
                                          : ''),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 15),
                                      isDense: true,
                                      isCollapsed: true,
                                      fillColor: Colors.white,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 20),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.params.value.minInventory =
                                        int.tryParse(text);
                                  },
                                ))),
                        Container(
                          width: 50,
                          alignment: Alignment.center,
                          child: Text(
                            '~',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color0000),
                          ),
                        ),
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  keyboardType: TextInputType.number,
                                  controller: TextEditingController(
                                      text: controller
                                                  .params.value.maxInventory !=
                                              null
                                          ? controller.params.value.maxInventory
                                              .toString()
                                          : ''),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 15),
                                      isDense: true,
                                      isCollapsed: true,
                                      fillColor: Colors.white,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 20),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.params.value.maxInventory =
                                        int.tryParse(text);
                                  },
                                ))),
                      ]))
                ],
              )),
            ),
            SizedBox(
              height: limitWidth,
              child: DrawUnderLine(
                  child: Row(
                children: [
                  Expanded(
                      child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '更换数量',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    ),
                  )),
                  Container(
                      width: maxWidth,
                      alignment: Alignment.centerLeft,
                      child: Row(children: [
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  keyboardType: TextInputType.number,
                                  controller: TextEditingController(
                                      text: controller.params.value.minChange !=
                                              null
                                          ? controller.params.value.minChange
                                              .toString()
                                          : ''),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 15),
                                      isDense: true,
                                      isCollapsed: true,
                                      fillColor: Colors.white,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 20),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.params.value.minChange =
                                        int.tryParse(text);
                                  },
                                ))),
                        Container(
                          width: 50,
                          alignment: Alignment.center,
                          child: Text(
                            '~',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color0000),
                          ),
                        ),
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  keyboardType: TextInputType.number,
                                  controller: TextEditingController(
                                      text: controller.params.value.maxChange !=
                                              null
                                          ? controller.params.value.maxChange
                                              .toString()
                                          : ''),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 15),
                                      isDense: true,
                                      isCollapsed: true,
                                      fillColor: Colors.white,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 20),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.params.value.maxChange =
                                        int.tryParse(text);
                                  },
                                ))),
                      ]))
                ],
              )),
            ),
          ],
        ),
      ),
      extendBody: true,
      bottomSheet: Container(
        height: limitWidth,
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
                top: BorderSide(
                    color: ColorsUtils.parseColorFromHexString(
                        ColorsUtils.colorE4e4)))),
        child: Row(
          children: [
            Expanded(
                child: Center(
              child: TextButton(
                  onPressed: () {},
                  child: Text(
                    '重置',
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color0052, fontSize: 16),
                  )),
            )),
            Expanded(
                child: Container(
              alignment: Alignment.center,
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
              child: TextButton(
                onPressed: () {},
                style: ButtonStyle(
                  padding: MaterialStateProperty.all(EdgeInsets.zero),
                ),
                child: Text(
                  '确定',
                  style: StylesUtils.customTextStyle(fontSize: 16),
                ),
              ),
            ))
          ],
        ),
      ),
    );
  }

  double get maxWidth {
    return 260.0;
  }

  double get limitWidth {
    return 50.0;
  }
}
